<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">账号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="uaccount" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="uname" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

<!--        表操作行-->
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

<!--        行操作栏-->
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="groupRole">分组</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            {{#  if(d.ustatus===false){ }}
                <a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete" lay-event="ice">冻结</a>
            {{#  } else { }}
                <a class="layui-btn layui-btn-xs layui-btn-normal data-count-delete" lay-event="ice">解冻</a>
            {{#  } }}
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="resetpassword">重置密码</a>
        </script>

<!--        这是一个模板语言用来处理状态-->
        <script type="text/html" id="ustatustemplet">
            {{#  if(d.ustatus===false){ }}
                <button type="button" class="layui-btn layui-btn-xs layui-btn-normal">正常使用</button>
            {{#  } else { }}
                <button type="button" class="layui-btn layui-btn-xs layui-btn-disabled">账号注销</button>
            {{#  } }}
        </script>
    </div>
    <!--添加页面与修改页面功能相似-->
    <div id="saveDiv" class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">账号</label>
            <div class="layui-input-block">
                <input type="text" name="uaccount" lay-verify="required" lay-reqtext="账号不能为空" placeholder="请输入账号" value="" class="layui-input">
                <tip>填写自己账号的名称。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="uname" lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名" value="" class="layui-input">
                <tip>填写自己姓名。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">密码</label>
            <div class="layui-input-block">
                <input type="password" name="upassword" lay-verify="required" lay-reqtext="密码不能为空" placeholder="请输入密码" value="" class="layui-input">
                <tip>填写自己密码。</tip>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>

    <div id="updateDiv" class="layui-form layuimini-form" lay-filter="updateform">
        <input type="hidden" name="uid" />
        <div class="layui-form-item">
            <label class="layui-form-label required">账号</label>
            <div class="layui-input-block">
                <input type="text" name="uaccount" lay-verify="required" lay-reqtext="账号不能为空" placeholder="请输入账号" value="" class="layui-input">
                <tip>填写自己账号的名称。</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="uname" lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名" value="" class="layui-input">
                <tip>填写自己姓名。</tip>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="updateBtn">确认保存</button>
            </div>
        </div>
    </div>


    <div id="groupRole" class="layui-form layuimini-form" lay-filter="groupRole">
        <input type="hidden" name="uid" value="" />
        <div class="layui-form-item">
            <div class="layui-input-block">
                <div id="test1"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <a class="layui-btn layui-btn-lg layui-btn-normal" lay-submit lay-filter="saveGroupRole">保存</a>
            </div>
        </div>
    </div>
</div>
<script src="../../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','transfer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            transfer = layui.transfer,
            table = layui.table;

        $("#saveDiv").hide();
        $("#updateDiv").hide();
        $("#groupRole").hide();
        table.render({
            id:"mytable",
            elem: '#currentTableId',
            url: '/rbac/user/page',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'uid', title: 'ID', sort: true, fixed: 'left'}
                ,{field: 'uaccount', title: '账号'}
                ,{field: 'uname', title: '姓名',sort: true}
                ,{field: 'upassword', title: '密码'}
                ,{field: 'ustatus', title: '状态',templet:'#ustatustemplet'}
                ,{title: '操作', minWidth: 320, toolbar: '#currentTableBar', fixed: 'right'}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('mytable', {
                url: '/rbac/user/page'

                ,page:{
                    curr:1
                }
                ,where:data.field
             });
            return false;
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                method:"post"
                ,url:"/rbac/user/saveForPassword"
                ,data: JSON.stringify(data.field)  //向后台传递多组参数
                ,contentType:"application/json;charset=UTF-8"
                ,success:function (d){
                    if(d){
                        // 成功
                        layer.msg("删除成功！！！",{icon:1,time:1000},function (){
                            layer.closeAll();
                            //应该对当前页面里面的表格进行刷 新操作
                            table.reload('mytable', {url: '/rbac/user/page'});
                        });

                    }else{
                        // 失败
                        layer.msg("删除失败！！！",{icon:2,time:1000},function (){
                            layer.closeAll();
                        });
                    }
                }
            });
            return false;
        });

        //监听修改操作
        form.on('submit(updateBtn)', function (data) {
            let vals = data.field;
            $.ajax({
                method:"put",  //请求方式 get post put delete
                url:"/rbac/user/updateUserInfo",// 请求地址  requesturl
                data:vals,
                success:function (d){
                    if(d){
                        // 成功
                        layer.msg("修改成功！！！",{icon:1,time:1000});
                        //应该对当前页面里面的表格进行刷 新操作
                        table.reload('mytable', {url: '/rbac/user/page'});
                    }else{
                        // 失败
                        layer.msg("修改失败！！！",{icon:2,time:1000});
                    }
                }
            });
            layer.closeAll();
            return false;
        });


        //分组时的添加操作
        form.on('submit(saveGroupRole)',function (data){
            //取到右侧全部数据
            var getData = transfer.getData('demo1');

            //将提交数组转换成为字符串
            if(getData.length > 0){
                let ps = "";
                for(var i = 0 ;i < getData.length;i++){
                    ps += ("&rids=" + getData[i]["value"]);
                }
                //"&rids=1&rids=2&rids=3"
                $.ajax({
                    method:"post"
                    ,async:false  // 完成同步操作
                    ,url:"/rbac/role/userForRole/"
                    ,data:"uid=" + $("[name='uid']").val() + ps
                    ,success:function (d){
                        if(d){
                            // 成功
                            layer.msg("分组成功！！！",{icon:1,time:1000},function (){
                                layer.closeAll();
                                //应该对当前页面里面的表格进行刷 新操作
                                table.reload('mytable', {url: '/rbac/user/page'});
                            });

                        }else{
                            // 失败
                            layer.msg("分组失败！！！",{icon:2,time:1000},function (){
                                layer.closeAll();
                            });
                        }
                    }
                });
            }else{
                layer.msg("此项操作将会使当前用户没有办法登陆系统，请保留一个角色，如果不想使用当前用户，请直接冻结!!"
                    ,{icon:2,time:2000,anmi:6})
            }
            return false;
        });

        /**
         * toolbar监听事件 表操作行
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: $("#saveDiv"),
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus(obj.config.id)
                    , data = checkStatus.data;

                if(data.length > 0){
                    //js 函数  相当于  java 方法
                    layer.confirm("您确认删除选中的数据吗?!",function (index){

                        var arr = [];
                        for(let i = 0 ;i < data.length;i++){
                            arr[arr.length] = data[i].uid;
                        }


                        $.ajax({
                            method:"post",
                            contentType:"application/json;charset=utf-8",
                            datatype: "json",
                            data:JSON.stringify(arr),
                            url:"/rbac/user/dels",
                            success:function (d){
                               if(d){
                                   // 成功
                                   layer.msg("删除成功！！！",{icon:1,time:1000});
                                   //应该对当前页面里面的表格进行刷 新操作
                                   table.reload('mytable', {url: '/rbac/user/page'});
                               }else{
                                   // 失败
                                   layer.msg("删除失败！！！",{icon:2,time:1000});
                               }
                                layer.close(index);
                            }
                        });

                    });
                }else{
                    layer.msg("请选择您要删除的数据！？？",{icon:2,anim:6});
                }
            }
        });

        //这个是行操作栏
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑用户',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: $("#updateDiv"),
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });

                //赋值
                form.val('updateform',{
                    "uid":data.uid,
                    "uaccount":data.uaccount,
                    "uname":data.uname
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        method:"delete",  //请求方式 get post put delete
                        url:"/rbac/user/del/"+data.uid,// 请求地址  requesturl
                        success:function (d){
                            if(d){
                                // 成功
                                layer.msg("删除成功！！！",{icon:1,time:1000});
                                //应该对当前页面里面的表格进行刷 新操作
                                table.reload('mytable', {url: '/rbac/user/page'});
                            }else{
                                // 失败
                                layer.msg("删除失败！！！",{icon:2,time:1000});
                            }
                        }
                    });
                    layer.close(index);
                });
            }else if (obj.event === 'ice'){
                layer.confirm('确认冻结当前用户吗!?',function (index){
                    $.ajax({
                        method:"put",  //请求方式 get post put delete
                        url:"/rbac/user/updateIce/"+data.uid+"/"+(data.ustatus==0?1:0),// 请求地址  requesturl
                        success:function (d){
                            if(d){
                                // 成功
                                layer.msg("冻结成功！！！",{icon:1,time:1000});
                                //应该对当前页面里面的表格进行刷 新操作
                                table.reload('mytable', {url: '/rbac/user/page'});
                            }else{
                                // 失败
                                layer.msg("冻结成功！！！",{icon:2,time:1000});
                            }
                        }
                    });
                    layer.close(index);
                });
            }else if(obj.event === "resetpassword"){
                $.ajax({
                    method:"put",  //请求方式 get post put delete
                    url:"/rbac/user/resetPassword",// 请求地址  requesturl
                    data:{uid:data.uid},
                    success:function (d){
                        if(d){
                            // 成功
                            layer.msg("重置成功！！！",{icon:1,time:1000});
                            //应该对当前页面里面的表格进行刷 新操作
                            table.reload('mytable', {url: '/rbac/user/page'});
                        }else{
                            // 失败
                            layer.msg("重置失败！！！",{icon:2,time:1000});
                        }
                    }
                });
            }else if(obj.event === 'groupRole'){
                //打开分组页面
                var index = layer.open({
                    title: '用户分组',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: $("#groupRole"),
                });
                $("[name='uid']").val(data.uid);
                //全部角色数据
                let trandata=[];

                //右侧角色数据
                let right = [];

                //生成页面穿梭框
                transfer.render({
                    title:["全部角色","已有角色"]
                    ,elem: '#test1'  //绑定元素
                    ,id: 'demo1' //定义索引
                });

                //后台同步数据 左侧数据
                $.ajax({
                    method:"get"
                    ,async:false  // 完成同步操作
                    ,url:"/rbac/role/showall"
                    ,success:function (d){
                        trandata = d;
                    }
                });

                $.ajax({
                    method:"get"
                    ,async:false  // 完成同步操作
                    ,url:"/rbac/role/showRoleIdForUid/"+data.uid
                    ,success:function (d){
                        right = d;
                    }
                });

                //不应该是后台查询出来当前用户有的所有角色 id
                transfer.reload('demo1',{
                    width:400
                    ,height:300
                    ,data: trandata  //[value:1  value:2 value : 3]
                    //1代表的是？所有角色里面的某一个角色id为1的角色
                    ,value:right //[1,2]
                    ,parseData: function(res) {
                        console.log(res,res[0],JSON.stringify(res));
                        return {
                            "value": res.rid //数据值
                            , "title": res.rname //数据标题
                            , "disabled": ""  //是否禁用
                            , "checked": "" //是否选中
                        }
                    }
                });
            }
        });

    });
</script>

</body>
</html>